package dao;

import java.io.Serializable;
import java.util.List;

import modelo.Pais;
import modelo.Provincia;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class ProvinciaDao implements Serializable {

	private static final long serialVersionUID = -2433596977523898587L;

	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	public ProvinciaDao() {
		super();
	}

	public List<Provincia> obtenerProvincias() {
		session = sessionFactory.openSession();

		Query query = session.createQuery("SELECT p FROM Provincia p");
		List<Provincia> prov = query.list();
//		for (Provincia p : prov) {
//			Hibernate.initialize(p.getIdProvincia());
//		}
		session.close();

		return prov;
	}

	public void guardarProvincia(Provincia p) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(p);
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarProvincia(Provincia p) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(p);
		session.getTransaction().commit();
		session.close();
	}

	public List<Provincia> obtenerProvincia(Pais pais) {
		session = sessionFactory.openSession();

		Query query = session.createQuery("from Provincia where IdPais = :id ");
		query.setParameter("id", pais.getIdPais());

		List<Provincia> list = query.list();
		
		return list;

	}
}
